package myqueuegather;

public class MyCircularQueue {
    public int[] elements;// 默认初始值为 null
    public int front;// 队头下标
    public int rear;// 队尾下标
    public MyCircularQueue(int k) {
        this.elements = new int[k+1];// 初始数组的容量
    }

    // 入队
    public boolean enQueue(int value) {
        if(isFull()){
            return  false;
        }
        this.elements[rear] = value;
        rear = (rear+1)% elements.length;
        return true;
    }

    public boolean deQueue() {
        if(isEmpty()){
            return  false;
        }
        front = (front + 1) % this.elements.length;
        return true;
    }

    public int Front() {
        if(isEmpty()){
            return -1;
        }
        return this.elements[front];
    }

    public int Rear() {
        if(isEmpty()){
            return -1;
        }
        int index = 0;
        if(rear == 0) {
            index = this.elements.length - 1;
        }else {
            index = rear -1;
        }
        return this.elements[index];
    }

    public boolean isEmpty() {
        return this.front == this.rear;
    }

    public boolean isFull() {
        // rear 下一个位置 是front，表示循环队列满了
        if((this.rear+1)%this.elements.length == front){
            return true;
        }
        return  false;
    }
}
